package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.School;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.University;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ISchoolService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IUniversityService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("School")
public class SchoolController {
    @Autowired
    private ISchoolService schoolService;
    @Resource
    private IUniversityService universityService;

    @RequestMapping(value = {"/toAddSchool"})
    public String toAddSchool(Model model) {
        List<University> universities = universityService.queryAllUniversity();
        model.addAttribute("universities", universities);
        return "toaddschool";
    }

    @RequestMapping(value = {"/toUpdateSchool"})
    public ModelAndView toUpdateSchool(Integer id) {
        //int id = Integer.parseInt(model.getAttribute("id").toString());
        School school = schoolService.querySchoolById(id);
        ModelAndView model = new ModelAndView("toupdateschool");

        List<University> universities = universityService.queryAllUniversity();
        model.addObject("universities", universities);
        model.addObject("school", school);
        return model;
    }

    @ResponseBody
    @RequestMapping("insertSchool")
    public String insertSchool(String name, String abbreviation, Integer universityId,String note)
    {
        School school = new School(null,name,abbreviation,universityId,note);
        schoolService.insertSchool(school);
        return "OK";
    }

    @RequestMapping("queryAllSchool")
    public String queryAllSchool(Model model,@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,
                                 @RequestParam(value = "pageSize",defaultValue = "5") int pageSize)
    {
        int totalCount = schoolService.queryTotalSchoolCount();
        //  总条数/页数再向上取整
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);

        List<Map<String,Object>> schools = new ArrayList<Map<String,Object>>();
        List<School> schools_ = schoolService.querySchoolPage(pageNow, pageSize);

        if(schools != null && schools_.size() > 0){
            for (School school_ : schools_) {
                Map<String,Object> school = new HashMap<>();
                University university = universityService.queryUniversityById(school_.getUniversityId());
                school.put("id",school_.getId());
                school.put("name",school_.getName());
                school.put("abbreviation",school_.getAbbreviation());
                school.put("university",university.getName());
                school.put("note",school_.getNote());
                model.addAttribute("totalPages", totalPages);
                model.addAttribute("pageNow", pageNow);
                model.addAttribute("pageSize", pageSize);
                schools.add(school);
            }
        }

        model.addAttribute("schools", schools);
        return "school";
    }



    @ResponseBody
    @GetMapping("querySchoolById")
    public School querySchoolById(Integer id)
    {
        return schoolService.querySchoolById(id);
    }

    @ResponseBody
    @RequestMapping("deleteSchoolById")
    public String deleteSchoolById(Integer id)
    {
        schoolService.deleteSchoolById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateSchoolById")
    public String updateSchoolById(Integer id,String name,String abbreviation,Integer universityId,String note)
    {
        School school = new School(id,name,abbreviation,universityId,note);
        schoolService.updateSchoolById(school);
        return "OK";
    }


}
